
package common;

import Swing.ReservationScreen;
import dao.CarDao;
import dao.ReservationDao;
import dao.StatusDao;
import entities.Car;
import entities.Reservation;
import enums.CarEnum;
import enums.ReservationEnum;
import enums.StatusEnum;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.lang.StringUtils;
import org.joda.time.LocalDate;


public class ReservationUtils{
    ReservationDao reservationDao = new ReservationDao();
    CarDao carDao = new CarDao();
    StatusDao statusDao = new StatusDao();
    private static Logger log = Logger.getLogger(ReservationUtils.class.getName());

    
     public List<LocalDate> getDateRange(LocalDate start, LocalDate end) {
        List<LocalDate> ret = new ArrayList<>();
        LocalDate tmp = start;
        while(tmp.isBefore(end) || tmp.equals(end)) {
            ret.add(tmp);
            tmp = tmp.plusDays(1);
        }
        return ret;
    }    
    
    public List<LocalDate> generateDatesInRange(){
        LocalDate start = LocalDate.now();
        LocalDate end = LocalDate.now().plusDays(30);
        return getDateRange(start, end);
    }
 
    public void saveReservation(HashMap<String, String> map){
        try{
            ReservationScreen reservationScreen = new ReservationScreen();
            Car car = carDao.findById(Integer.parseInt(map.get(CarEnum.carId.name())));
            Reservation reservation = new Reservation();
            reservation.setDescription(map.get(ReservationEnum.description.name()));
            reservation.setRepairDate(new LocalDate(map.get(ReservationEnum.repairDate.name())).toDateTimeAtStartOfDay().toDate());
            reservation.setCar(car);
            reservation.setCustomer(car.getCustomer());
            reservation.setStatus(statusDao.getByCode(StatusEnum.ACC));
            int response = reservationDao.saveReservation(reservation);
            if(response == 1){
                reservationScreen.showResponseAfterSaveReservation("Rezerwacja została zapisana pomyślnie", response);
            }
            else{
                reservationScreen.showResponseAfterSaveReservation("Rezerwacja nie została zapisana", response);
            }
        }
  
        catch(Exception e){
            log.log(Level.SEVERE, e.getMessage(), e);
        }
    }
    
    public String checkReservationAtRepairDate(String repairDate){
        try{
            if(StringUtils.isNotEmpty(repairDate)){
                int repairCount = reservationDao.getReservationCountByRepairDate(new LocalDate(repairDate).toDateTimeAtStartOfDay().toDate());
                if(repairCount > 0){
                    return "Ilość napraw w dniu " + repairDate + " wynosi " + repairCount +
                            "\nCzy mimo to zapisać termin naprawy?";
                }
            }
        }
        catch(Exception e){
            log.log(Level.SEVERE, e.getMessage(), e);
        }
        return "";
    }

    public ArrayList<Reservation> getReservationData(){
         return reservationDao.getAllReservationData();
    }
    
    public Reservation getReservationDetails(int reservationId){
        return reservationDao.getReservationById(reservationId);
    }

    public void deleteReservation(int reservationId){
        Reservation reservation = reservationDao.getReservationById(reservationId);
        reservation.setStatus(statusDao.getByCode(StatusEnum.CAN));
        reservationDao.update(reservation);
    }
    
    public void updateReservation(Reservation reservation){
        reservationDao.update(reservation);
    }
}
